package com.scau.productservice.serviceImpl;

import com.scau.productservice.dao.ProductDao;
import model.Product;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import service.ProductService;
@DubboService
public class ProductServiceImpl implements ProductService {
    @Autowired
    ProductDao productDao;
    @Override
    public void addProduct(Product product) {
        productDao.addProduct(product);
    }

    @Override
    public Product getById(String id) {
        return productDao.getById(id);
    }

    @Override
    public void updateSold(Product product) {
        int result = productDao.updateSold(product);
        if(result == 0){
            throw new RuntimeException("抢购失败");
        }
    }
    //库存校验
    public Product checkStock(String id){
        Product product = getById(id);
        if(product.getStock() == product.getSold()){
            throw new RuntimeException("商品id为"+id+"的商品库存不足");
        }
        return product;
    }
}
